<?php

namespace app\index\controller;

use think\App;
use think\console\Table;
use think\Controller;
use think\Db;

class Myorder extends Base
{
    public $wxjsconfig;
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->wxjsconfig=$this->wxbase->jssdk->buildConfig(['chooseWXPay'], $debug = false, $beta = false, $json = true);

    }
    public function index(){
        if($this->isLogin()){
           $userStatus=\db('user')->where('user_id',$this->getUserId())->value('user_status');
        }else{
            $userStatus=null;
        }
        $order['payed']=db('order')->where('order_user_id',$this->getUserId())->where('order_status','>=',1)->where('order_status','<=',2)->order('order_ctime','desc')->select();
        $order['nopayed']=db('order')->where('order_user_id',$this->getUserId())->where('order_status',0)->order('order_ctime','desc')->select();
        $order['over']=db('order')->where('order_user_id',$this->getUserId())->where('order_status','>',2)->order('order_ctime','desc')->select();
        $order['tuple']=Db::table('lg_tuple')
                        ->alias('a')
                        ->join('lg_order b ','b.order_id = a.tuple_order_id')
                        ->where('a.tuple_user_id',$this->getUserId())
                        ->order('a.tuple_ctime','desc')
                        ->select();
        foreach ($order['tuple'] as $k=>$v){
            $order['tuple'][$k]['userMsg']=\db('user')->find($v['order_user_id']);
        }
        $this->assign('order',$order);
        $this->assign('userStatus',$userStatus);
        return $this->fetch();
    }
    public function showOrder(){
        if(input('param.order_id')){
            $order=Db::table('lg_order')
                ->alias('a')
                ->join('lg_user b ','b.user_id= a.order_user_id')
                ->where('a.order_id',input('param.order_id'))
                ->find();
        }
        if(input('param.order_num')){
            $order=Db::table('lg_order')
                ->alias('a')
                ->join('lg_user b ','b.user_id= a.order_user_id')
                ->where('a.order_num',input('param.order_num'))
                ->find();
        }
        if(!$order){
            	$this->redirect('myorder/index');
            }

        $order['tuple_user_id']=\db('tuple')->where('tuple_order_id',$order['order_id'])->value('tuple_user_id');
        $order['login_user_id']=$this->getUserId();
        $user=session('user');
        $userStatus=db('user')->where('user_id',$user['user_id'])->value('user_status');
        if($userStatus==2){
            $this->assign('tuple',1);
            $this->assign('order',$order);
            $this->assign('wxjsconfig',$this->wxjsconfig);
        }else{
            if($order['order_user_id']!=$this->getUserId()){
                $this->error("请先申请成为送餐员吧",'orderhall/index');
            }else{
                $this->assign('tuple',0);
                $this->assign('order',$order);
                $this->assign('wxjsconfig',$this->wxjsconfig);
            }
        }
        return $this->fetch();
    }
    public function checkOrder(){
        if(!$this->isLogin()){
           return $this->error("请先申请成为送餐员吧",'orderhall/index');
        }
        $user=session('user');
        $userStatus=db('user')->where('user_id',$user['user_id'])->value('user_status');
        if($userStatus==2){
           return $this->success("ok");
        }
        $order_num=input('param.order_num');
        $order=db('order')->where('order_num',$order_num)->find();
        if($order['order_user_id']!=$this->getUserId()){
           return $this->error("请先申请成为送餐员吧",'orderhall/index');
        }else{
           return $this->success("ok");
        }
    }
}

